; start.asm
; Bootloader calls us... here we go

section .text
bits 32

;multiboot.. These were fucked up in my last version
%define MULTIBOOT_HEADER_MAGIC	0x1BADB002
%define MULTIBOOT_HEADER_FLAGS	0x00010003

;Ok.. now we need to locate our Kernel.
%define KERNEL_VMA_BASE			0x00100000
%define KERNEL_LMA_BASE			0x00100000

;the GDT for the kernel..
%define CS_KERNEL				0x10
%define CS_KERNEL32				0x08

;Thank ya Mr. linker.ld
extern _edata
extern _end
extern _boot

;extern for main (in kmain.d)
extern main

;Can't forget our stack
%define STACK_SIZE				0x4000 ;16k... ;)

;Here's where we start! :)
global start
global _start
	; align the multiboot header to a 32 bit boundary
	align 4

	multiboot_header:

	dd MULTIBOOT_HEADER_MAGIC
	dd MULTIBOOT_HEADER_FLAGS
	dd -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
	dd multiboot_header
	dd _boot
	dd _edata
	dd _end
	dd _start

;Our entry point! :)
start:
_start:

	;No interrupts for you!
	cli

	;point our stack pointer to the bottom of the stack... DUH
	mov esp, stack+STACK_SIZE

	;Push out our multiboot info to the kernel
	push ebx
	push eax

	;enable SSE
	mov ecx, cr0
	btr ecx, 2	; clear CR0.EM bit
	bts ecx, 1	; set CR0.MP bit
	mov cr0, ecx

	mov ecx, cr4
	bts ecx, 9	; set CR4.OSFXSR bit
	bts ecx, 10	; set CR4.OSXMMEXCPT bit
	mov cr4, ecx

	; call main
	call main

_halt:

	cli

	hlt
	jmp _halt

	nop
	nop

section .bss
align 32
stack:
	resb STACK_SIZE
